package com.wzk.travelspringboot.service.impl;

import com.wzk.travelspringboot.dao.BlogDAO;
import com.wzk.travelspringboot.dao.TicketOrderDAO;
import com.wzk.travelspringboot.dao.UserDAO;
import com.wzk.travelspringboot.dto.Result;
import com.wzk.travelspringboot.dto.ScenicDTO;
import com.wzk.travelspringboot.entity.Scenic;
import com.wzk.travelspringboot.entity.TicketOrder;
import com.wzk.travelspringboot.service.CountService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.logging.Logger;

/**
 * @author wangzikang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CountServiceImpl implements CountService {

    private final UserDAO userDAO;

    private final TicketOrderDAO ticketOrderDAO;

    private final BlogDAO blogDAO;

    public static final Logger logger = Logger.getLogger(CountServiceImpl.class.getName());


    @Override
    public Result getCount() {
        //1.查询今日门票总收入
        //今天开始的时间
        LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
        //今天结束的时间
        LocalDateTime endOfDay = LocalDate.now().atStartOfDay().plusDays(1).minusNanos(1);
        List<TicketOrder> ticketOrders = ticketOrderDAO.queryToDayAmount(startOfDay, endOfDay);
        double todayAmount = 0;
        int usedCount = 0;
        if (!ticketOrders.isEmpty()) {
            for (TicketOrder order : ticketOrders) {
                todayAmount += order.getTotalAmount();
                if (order.getUsed()){
                    usedCount++;
                }
            }
        }
        //昨天的开始时间
        LocalDateTime startOfYesterday = LocalDate.now().minusDays(1).atStartOfDay();
        //昨天的结束时间
        LocalDateTime endOfYesterday = LocalDate.now().minusDays(1).atStartOfDay().plusDays(1).minusNanos(1);
        List<TicketOrder> yestDayOrders = ticketOrderDAO.queryToDayAmount(startOfYesterday, endOfYesterday);
        double yestdayAmount = 0;
        if (!yestDayOrders.isEmpty()) {
            for (TicketOrder order : yestDayOrders) {
                yestdayAmount += order.getTotalAmount();
            }
        }
        //2.统计用户数量 和 本月新增的用户数量
        //获取今天的年和月
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前年份
        int year = currentDate.getYear();
        // 获取当前月份
        int month = currentDate.getMonthValue();
        int totalUser = userDAO.userCount();
        int toMonthTotalUser = userDAO.userCountOfMonth(year, month);
        //3.统计今天的所有发布的动态数量 和 昨天的
        int toDayBlogTotal = blogDAO.toDayBlogTotal(startOfDay, endOfDay);
        int yestDayBlogTotal = blogDAO.toDayBlogTotal(startOfYesterday, endOfYesterday);
        //4.封装并返回
        HashMap<String, Object> res = new HashMap<>();
        res.put("todayAmount", todayAmount);
        res.put("usedCount", usedCount);
        res.put("yestdayAmount", yestdayAmount);
        res.put("yestdayBlogTotal", yestDayBlogTotal);
        res.put("totalUser", totalUser);
        res.put("toMonthTotalUser", toMonthTotalUser);
        res.put("toDayBlogTotal", toDayBlogTotal);
        return Result.success("统计加载成功",res);
    }

    @Override
    public Result getWeekData() {
        //1.获取一周内的数据(周一到今天)
        List<TicketOrder> ticketOrders = ticketOrderDAO.queryCurrWeek();
        //2.记录每天的数据
        ArrayList<String> weekStrList = new ArrayList<>();
        ArrayList<Double> weekAmountList = new ArrayList<>();
        //3.解析数据
        for (TicketOrder ticketOrder : ticketOrders) {
            //取出星期字符串
            String weekStr = ticketOrder.getCreateTime().getDayOfWeek().toString();
            //获取星期的中文
            String chineseWeek = getChineseWeek(weekStr);
            //判断集合中是否存在
            int i = weekStrList.indexOf(chineseWeek);
            if (i == -1){
                // 如果星期不存在，添加到列表
                weekStrList.add(chineseWeek);
                weekAmountList.add(ticketOrder.getTotalAmount());
            }else {
                //星期存在累加金额
                // 如果星期已存在，更新金额
                double updatedAmount = weekAmountList.get(i) + ticketOrder.getTotalAmount();
                weekAmountList.set(i, updatedAmount);
            }
        }
        HashMap<String, Object> res = new HashMap<>();
        res.put("keys", weekStrList);
        res.put("values", weekAmountList);
        return Result.success(res);
    }

    //获取星期的中文
    public String getChineseWeek(String englishWeek) {
        // 创建英文星期与中文星期的映射
        Map<String, String> weekMap = new HashMap<>();
        weekMap.put("MONDAY", "星期一");
        weekMap.put("TUESDAY", "星期二");
        weekMap.put("WEDNESDAY", "星期三");
        weekMap.put("THURSDAY", "星期四");
        weekMap.put("FRIDAY", "星期五");
        weekMap.put("SATURDAY", "星期六");
        weekMap.put("SUNDAY", "星期天");
        // 返回对应的中文星期
        return weekMap.getOrDefault(englishWeek, "未知");
    }
}
